<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class overview extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model("m_bagian");
        $this->load->model("m_lokasi");
        $this->load->model("m_kategoribarang");
        $this->load->model("m_jenisbarang");
        $this->load->model("m_vendor");
        $this->load->model("m_milik");
        $this->load->model("m_merk");
        $this->load->model("m_personil");
        $this->load->model("m_ruang");
        // load library
        // load helpers
        $this->smarty->load_javascript("resource/js/fusioncharts/fusioncharts.js");
    }

    // welcome administrator
    public function index() {
        // set page rules (all admin)
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/overview/index.html");
        // data
        $this->smarty->assign("rs_today", $this->datetimemanipulation->get_date_indonesia(date("Y-m-d")));
        $this->smarty->assign("ttl_bagian", $this->m_bagian->get_count_all_bagian());
        $this->smarty->assign("ttl_lokasi", $this->m_lokasi->get_count_all_lokasi());
        $this->smarty->assign("ttl_kategoribarang", $this->m_kategoribarang->get_count_all_kategoribarang());
        $this->smarty->assign("ttl_vendor", $this->m_vendor->get_count_all_vendor());
        $this->smarty->assign("ttl_milik", $this->m_milik->get_count_all_milik());
        $this->smarty->assign("ttl_personil", $this->m_personil->get_count_all_personil());
        $kategori = $this->m_kategoribarang->get_all_kategoribarang();
        foreach ($kategori as $val) {
            $data_jenis[$val['kategoribarang_name']]['nama_kat']=$val['kategoribarang_name'];
            $data_jenis[$val['kategoribarang_name']]['total']=  $this->m_jenisbarang->get_count_jenisbarang_by_kategoribarang_cd($val['kategoribarang_cd']);
            
        }
        $this->smarty->assign("jenis", $data_jenis);
        foreach ($kategori as $vals) {
            $data_merk[$vals['kategoribarang_name']]['nama_kat']=$vals['kategoribarang_name'];
            $data_merk[$vals['kategoribarang_name']]['total']=  $this->m_merk->get_count_merk_by_kategoribarang_cd($vals['kategoribarang_cd']);
            
        }
        $this->smarty->assign("merk", $data_merk);
        $ruang = $this->m_milik->get_all_milik();
        foreach ($ruang as $value) {
            $data_ruang[$value['milik_name']]['nama_milik']=$value['milik_name'];
            $data_ruang[$value['milik_name']]['total']=  $this->m_ruang->get_count_ruang_by_milik_cd($value['milik_cd']);
            
        }
        $this->smarty->assign("ruang", $data_ruang);
        // output
        parent::display();
    }
    
    /*
     * CHART
     */
    
    public function data_xml_ruang() {
        // data
        $ruang = $this->m_milik->get_all_milik();
        $xml = "";
        $ttl = 0;
        foreach ($ruang as $value) {
            $total =  $this->m_ruang->get_count_ruang_by_milik_cd($value['milik_cd']);
            $xml .= '<set label="'.$value['milik_name'].'" value="'.$total.'" />';
            $ttl = $ttl + $total;
        }
        $max_html = 'yAxisMaxValue="'.($ttl + 10).'"';
        
        // data
        
        
        header("Content-type: text/xml");

        $data = '';
        $data .= '<chart 
                ' . $max_html . '
                showValues="0" 
                labelDisplay="ROTATE"
                slantLabels="1" 
                yAxisMinValue="0"
                formatNumberScale="0" 
                bgColor="FFFFFF"
                showBorder="0"
                thousandSeparator="."
                decimalPrecision="0" 
                limitsDecimalPrecision="0">';
        $data .= $xml;
        $data .= "</chart>";
        echo $data;
    }
    
    public function data_xml_master() {
        // data
        $ttl_bagian = $this->m_bagian->get_count_all_bagian();
        $ttl_lokasi = $this->m_lokasi->get_count_all_lokasi();
        $ttl_kategoribarang = $this->m_kategoribarang->get_count_all_kategoribarang();
        $ttl_vendor = $this->m_vendor->get_count_all_vendor();
        $ttl_milik = $this->m_milik->get_count_all_milik();
        $ttl_personil = $this->m_personil->get_count_all_personil();
        $ttl = $ttl_bagian + $ttl_lokasi + $ttl_kategoribarang + $ttl_vendor + $ttl_milik + $ttl_personil;
        $max_html = 'yAxisMaxValue="'.($ttl + 10).'"';
        
        // data
        
        
        header("Content-type: text/xml");

        $data = '';
        $data .= '<chart 
                ' . $max_html . '
                showValues="0" 
                yAxisName="Total Data"
                labelDisplay="ROTATE"
                slantLabels="1" 
                yAxisMinValue="0"
                formatNumberScale="0" 
                bgColor="FFFFFF"
                showBorder="0"
                thousandSeparator="."
                decimalPrecision="0" 
                limitsDecimalPrecision="0">';
        $data .= '<set label="Bagian" value="'.(!empty($ttl_bagian)?$ttl_bagian:0).'" />
                  <set label="Lokasi" value="'.(!empty($ttl_lokasi)?$ttl_lokasi:0).'" />
                  <set label="Kategori" value="'.(!empty($ttl_kategoribarang)?$ttl_kategoribarang:0).'" />
                  <set label="Vendor" value="'.(!empty($ttl_vendor)?$ttl_vendor:0).'" />
                  <set label="Pemilik" value="'.(!empty($ttl_milik)?$ttl_milik:0).'" />
                  <set label="Personil" value="'.(!empty($ttl_personil)?$ttl_personil:0).'" />';
        $data .= "</chart>";
        echo $data;
    }

}